var t = 0, a = !0, e = 1, i = !1, o = [];

Page({
    data: {
        WindowWidth: getApp().core.getSystemInfoSync().windowWidth,
        WindowHeight: getApp().core.getSystemInfoSync().windowHeight,
        left: 0,
        show_notice: -1,
        animationData: {},
        play: -1,
        time: 0,
        buy: !1,
        opendate: !1,
        goods: "",
        form: {
            number: 1
        },
        time_all: [],
        num:8,//这是当前有几个人
        jindutiao: [  //这个是进度条的标准，刻度
        {
        num:3,
        price:'999'
        },
        {
            num: 10,
            price: '599'
        }
        ],
        
    },
    onLoad: function(t) {
        
        getApp().page.onLoad(this, t), t.page_id || (t.page_id = -1), this.setData({
            options: t
        }), this.loadData(t);
        this.jindutiao();
        var that = this;
        wx.getLocation({
            type: 'wgs84',
            success (res) {
                 var latitude = res.latitude
                 var longitude = res.longitude
                 var speed = res.speed
                 var accuracy = res.accuracy
                console.log(res)
                var qqMapApi = 'https://apis.map.qq.com/ws/geocoder/v1/' + "?location=" + latitude + ',' + longitude + "&key=" + 'F2JBZ-QTUWX-W6G47-7XDRH-US4UE-WIB22' + "&get_poi=1";
                that.sendRequest(qqMapApi);
            }
        });
        
    },
    suspension: function() {
        var a = this;
        t = setInterval(function() {
            getApp().request({
                url: getApp().api.default.buy_data,
                data: {
                    time: a.data.time
                },
                method: "POST",
                success: function(t) {
                    if (0 == t.code) {
                        var e = !1;
                        a.data.msgHistory == t.md5 && (e = !0);
                        var i = "", o = t.cha_time, s = Math.floor(o / 60 - 60 * Math.floor(o / 3600));
                        i = 0 == s ? o % 60 + "秒" : s + "分" + o % 60 + "秒", !e && t.cha_time <= 300 ? a.setData({
                            buy: {
                                time: i,
                                type: t.data.type,
                                url: t.data.url,
                                user: t.data.user.length >= 5 ? t.data.user.slice(0, 4) + "..." : t.data.user,
                                avatar_url: t.data.avatar_url,
                                address: t.data.address.length >= 8 ? t.data.address.slice(0, 7) + "..." : t.data.address,
                                content: t.data.content
                            },
                            msgHistory: t.md5
                        }) : a.setData({
                            buy: !1
                        });
                    }
                },
                noHandlerFail: !0
            });
        }, 1e4);
    },
    loadData: function() {
        var t = this, e = {}, i = t.data.options;
        if (-1 != i.page_id) e.page_id = i.page_id; else {
            e.page_id = -1;
            var s = getApp().core.getStorageSync(getApp().const.PAGE_INDEX_INDEX);
            s && (s.act_modal_list = [], t.setData(s));
        }
        getApp().request({
            url: getApp().api.default.index,
            data: e,
            success: function(e) {
                if (0 == e.code) {
                    if ("diy" == e.data.status) {
                        var s = e.data.act_modal_list;
                        -1 != i.page_id && (getApp().core.setNavigationBarTitle({
                            title: e.data.info
                        }), t.setData({
                            title: e.data.info
                        }));
                        for (var n = s.length - 1; n >= 0; n--) (void 0 === s[n].status || 0 == s[n].status) && getApp().helper.inArray(s[n].page_id, o) && !t.data.user_info_show || 0 == s[n].show ? s.splice(n, 1) : o.push(s[n].page_id);
                        t.setData({
                            template: e.data.template,
                            act_modal_list: s,
                            time_all: e.data.time_all
                        }), t.setTime();
                    } else a ? t.data.user_info_show || (a = !1) : e.data.act_modal_list = [], t.setData(e.data), 
                    t.miaoshaTimer();
                    -1 == i.page_id && getApp().core.setStorageSync(getApp().const.PAGE_INDEX_INDEX, e.data);
                }
            },
            complete: function() {
                getApp().core.stopPullDownRefresh();
            }
        });
    },
    onShow: function() {
        var t = this;
        getApp().page.onShow(this), require("./../../components/diy/diy.js").init(this), 
        getApp().getConfig(function(a) {
            var e = a.store;
            e && e.name && -1 == t.data.options.page_id && getApp().core.setNavigationBarTitle({
                title: e.name
            }), e && 1 === e.purchase_frame ? t.suspension(t.data.time) : t.setData({
                buy_user: ""
            });
        }), getApp().query = null;
    },
    onPullDownRefresh: function() {
        getApp().getStoreData(), clearInterval(e), this.loadData();
    },
    onShareAppMessage: function(t) {
        getApp().page.onShareAppMessage(this);
        var a = this, e = getApp().getUser();
        return -1 != a.data.options.page_id ? {
            path: "/pages/index/index?user_id=" + e.id + "&page_id=" + a.data.options.page_id,
            title: a.data.title
        } : {
            path: "/pages/index/index?user_id=" + e.id,
            title: a.data.store.name
        };
    },
    showshop: function(t) {
        var a = this, e = t.currentTarget.dataset.id, i = t.currentTarget.dataset;
        getApp().request({
            url: getApp().api.default.goods,
            data: {
                id: e
            },
            success: function(t) {
                0 == t.code && a.setData({
                    data: i,
                    attr_group_list: t.data.attr_group_list,
                    goods: t.data,
                    showModal: !0
                });
            }
        });
    },
    miaoshaTimer: function() {
        var t = this;
        t.data.miaosha && 0 != t.data.miaosha.rest_time && (t.data.miaosha.ms_next || (e = setInterval(function() {
            t.data.miaosha.rest_time > 0 ? (t.data.miaosha.rest_time = t.data.miaosha.rest_time - 1, 
            t.data.miaosha.times = t.setTimeList(t.data.miaosha.rest_time), t.setData({
                miaosha: t.data.miaosha
            })) : clearInterval(e);
        }, 1e3)));
    },
    onHide: function() {
        getApp().page.onHide(this), this.setData({
            play: -1
        }), clearInterval(t);
    },
    onUnload: function() {
        getApp().page.onUnload(this), this.setData({
            play: -1
        }), clearInterval(e), clearInterval(t);
    },
    showNotice: function(t) {
        console.log(t), this.setData({
            show_notice: t.currentTarget.dataset.index
        });
    },
    closeNotice: function() {
        this.setData({
            show_notice: -1
        });
    },
    to_dial: function() {
        var t = this.data.store.contact_tel;
        getApp().core.makePhoneCall({
            phoneNumber: t
        });
    },
    closeActModal: function() {
        var t = this, a = t.data.act_modal_list;
        for (var e in a) {
            var i = parseInt(e);
            a[i].show && (a[i].show = !1);
            break;
        }
        t.setData({
            act_modal_list: a
        }), setTimeout(function() {
            for (var e in a) if (a[e].show) {
                var i = a.splice(e, 1);
                a = i.concat(a);
                break;
            }
            t.setData({
                act_modal_list: a
            });
        }, 500);
    },
    naveClick: function(t) {
        var a = this;
        getApp().navigatorClick(t, a);
    },
    onPageScroll: function(t) {
        var a = this;
        if (!i && -1 != a.data.play) {
            var e = getApp().core.getSystemInfoSync().windowHeight;
            "undefined" == typeof my ? getApp().core.createSelectorQuery().select(".video").fields({
                rect: !0
            }, function(t) {
                (t.top <= -200 || t.top >= e - 57) && a.setData({
                    play: -1
                });
            }).exec() : getApp().core.createSelectorQuery().select(".video").boundingClientRect().scrollOffset().exec(function(t) {
                (t[0].top <= -200 || t[0].top >= e - 57) && a.setData({
                    play: -1
                });
            });
        }
    },
    fullscreenchange: function(t) {
        i = !!t.detail.fullScreen;
    },
    jindutiao: function () {
        var that = this;
        var jindutiao = that.data.jindutiao;
        var num = that.data.num;
            var p = 0;
            for (var i = 0; i < jindutiao.length; i++) {
              if (jindutiao[i].num > num) {
                p = i;
                break;
              }
            }
            if (i == jindutiao.length) { p = i; }
            for (var j = 0; j < p; j++) {
              jindutiao[j].statu = true;
            }
            if (p == 0) { 
              num = 0.5 / jindutiao.length * 100; 
              
            } else if (jindutiao[p - 1].num < num) {
              num = (p + 0.5) / jindutiao.length * 100;
              //毕竟不是当前进度等分，所以让他在等于8，7，6的时候也能在中间。就加0.5
            } else {
              num = p / jindutiao.length * 100;
              //当前黄色进度长度就是当前人数除以总人数乘以100，就是进度条宽度的百分比。
            }
            that.setData({
              width: num,
              p: p,
              jindutiao: jindutiao
            })
      },
      sendRequest:function(qqMapApi) {
        const that = this
        console.log(qqMapApi)
        wx.request({
          url: qqMapApi,
          header: {
            'Content-Type': 'application/json'
          },
          data: {},
          method:'GET',
          success: (res) => {
            console.log(res)
            if (res.statusCode == 200 && res.data.status == 0) {
              // 从返回值中提取需要的业务地理信息数据 国家、省、市、县区、街道
              that.setData({ nation: res.data.result.address_component.nation });
              that.setData({ province: res.data.result.address_component.province });
              that.setData({ city: res.data.result.address_component.city });
              that.setData({ district: res.data.result.address_component.district });
              that.setData({ street: res.data.result.address_component.street });
             
            }
          }
        })
      }
});